package com.qqtop10.manager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtop10.manager.model.Admin;
import com.qqtop10.manager.model.Category;
import com.qqtop10.manager.model.PagingArgs;
import com.qqtop10.manager.model.Result;
import com.qqtop10.manager.service.CategoryService;
import com.qqtop10.manager.utils.LogHelper;

@RequestMapping("/manager/category")
@Controller
public class CategoryController extends BaseController {

	@Autowired
	@Qualifier("categoryService")
	private CategoryService categoryService;

	@RequestMapping("tree.do")
	@ResponseBody
	public Result tree(Category input) {
		try {
			if (input.getId() == null) {
				input.setParentId(0);
			} else if (input.getParentId() == null) {
				input.setParentId(input.getId());
				input.setId(null);
			}
			PagingArgs pagingArgs = new PagingArgs();
			pagingArgs.set_limit(Integer.MAX_VALUE);
			return ok(categoryService.all(input, pagingArgs));
		} catch (Exception e) {
			return excep(e);
		}
	}

	@RequestMapping("get.do")
	@ResponseBody
	public Result get(Category input) {
		try {
			if (input == null || input.getId() == null && input.getUuid() == null && input.getName() == null) {
				return err("有些必须的参数没有指定。");
			}
			return ok(categoryService.get(input));
		} catch (Exception e) {
			return excep(e);
		}
	}

	@RequestMapping("add.do")
	@ResponseBody
	public Result add(Category input) {
		try {
			if (input == null || input.getName() == null
					|| input.getFull_name() == null || input.getLevel_type() == null) {
				return err("有些必须的参数没有指定。");
			}
			if (input.getParentId() == null || input.getParentUuid() == null) {
				input.setParentId(0);
				input.setParentUuid("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
			}
			input.setUuid(newId());
			input.setCreatetime(now());
			input.setCreator(getLogindUuid());
			if (categoryService.add(input)) {
				input.setId(categoryService.get(input).getId());
				
				Admin Tempadmin = (Admin) getRequest().getSession().getAttribute("Logind");
				LogHelper.Log(Tempadmin.getAccount(),"后台分类管理模块","添加成功",LogHelper.getLocalIp(getRequest()),Tempadmin.getAccount(),"",true);

				
				return ok("操作成功！", input);
			}
			return err("操作失败！");
		} catch (Exception e) {
			return excep(e);
		}
	}

	@RequestMapping("update.do")
	@ResponseBody
	public Result update(Category input) {
		try {
			if (input == null || input.getId() == null || input.getAttributes() == null || input.getName() == null
					|| input.getFull_name() == null || input.getLevel_type() == null) {
				return err("有些必须的参数没有指定。");
			}
			if (input.getParentId() == null || input.getParentUuid() == null) {
				input.setParentId(0);
				input.setParentUuid("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
			}
			input.setMender(getLogindUuid());
			input.setUpdatetime(now());
			if (categoryService.update(input)) {
				Admin Tempadmin = (Admin) getRequest().getSession().getAttribute("Logind");
				LogHelper.Log(Tempadmin.getAccount(),"后台分类管理模块","修改成功",LogHelper.getLocalIp(getRequest()),Tempadmin.getAccount(),"",true);

				return ok("操作成功！", input);
			}
			return err("操作失败！");
		} catch (Exception e) {
			return excep(e);
		}
	}

	@RequestMapping("delete.do")
	@ResponseBody
	public Result delete(Category input) {
		try {
			if (input == null || input.getId() == null) {
				return err("有些必须的参数没有指定。");
			}
			if (categoryService.delete(input)) {
				Admin Tempadmin = (Admin) getRequest().getSession().getAttribute("Logind");
				LogHelper.Log(Tempadmin.getAccount(),"后台分类管理模块","删除成功",LogHelper.getLocalIp(getRequest()),Tempadmin.getAccount(),"",true);

				return ok("操作成功！");
			}
			return err("操作失败！");
		} catch (Exception e) {
			return excep(e);
		}
	}
}
